
var feedmap ; // GMap2
var marker; // GMarker

if(!document.getElementsByClassName) {
	document.getElementsByClassName = function(classname,node) {
		var classElements = new Array();
		if ( node == null )
			node = document;
		var els = node.getElementsByTagName('*');
		var elsLen = els.length;
		var pattern = new RegExp('(^|\\s)'+classname+'(\\s|$)');
		for (i = 0, j = 0; i < elsLen; i++) {
			if ( pattern.test(els[i].className) ) {
				classElements[j] = els[i];
				j++;
			}
		}
		return classElements;
	};
}

function startup() {
      if (GBrowserIsCompatible()) {
	if (window.attachEvent) {window.attachEvent('unonload', GUnload);} else if (window.addEventListener) {window.addEventListener('unload', GUnload, false);} else {document.addEventListener('unload', GUnload, false);} 
	feedmap = new GMap2(document.getElementById("map_canvas"));
	feedmap.setCenter(new GLatLng(52.965287111110975,-1.1437797546386719), 13);
 	feedmap.setUIToDefault();
	feedmap.enableGoogleBar();
	marker = new_marker(new GLatLng(52.954236,-1.143823));
	marker_onclick( );
      }
}


function createMarker(input) {
	var m = new GMarker(input.point);
	GEvent.addListener(m, "click", function() { this.openInfoWindowHtml( formatWindow(input) );});
	return m;
}



function createTabbedMarker(input) {
	var m = new GMarker(input.point);
	var tabs_array = [ 
		new GInfoWindowTab("Who", "<div class=\"gbubble\">" + input.who + "<br>" + input.information + "</div>"),
		new GInfoWindowTab("Link", "<div class=\"gbubble\"><a href=\"" + input.link + "\" target=\"_blank\">" + input.link + "</a></div>") 
	];
	GEvent.addListener(m, "click", function() { m.openInfoWindowTabsHtml(tabs_array);
	});
	return m;
}

function parse_json (doc, response) {
	var jsonData = eval("(" + doc + ")");
	for (var i = 0; i < jsonData.markers.length; i++) {
		var marker = createTabbedMarker(jsonData.markers[i]);
		feedmap.addOverlay(marker);
	}
}

function set_form_latlng(lat, lng) {
	var u = document.getElementById("flat")
	if(u) u.value = lat
	u = document.getElementById("flng")
	if(u) u.value = lng
}

function marker_moved() {
	var ll = marker.getLatLng();
	set_form_latlng(ll.lat(), ll.lng());
}

function drag_start() {
	feedmap.closeInfoWindow();
}

function infowindow_text() {
	var t = document.getElementById('markertxt').value;
	if(t == "")
		t = '<p>Pin this marker on where your event will be.</p><p class="tight">Click the Google in the bottom left to search for the address / venue.</p>'
	return '<div class="gbubble tight">' + t.replace(/\n/g, '<br>') + '</div>'
}

function marker_onclick() {
	marker.openInfoWindowHtml(infowindow_text());
}


function new_marker(glatlng) {
	marker = new GMarker(glatlng, {draggable: true});
	GEvent.addListener(marker, "dragstart", drag_start);
	GEvent.addListener(marker, "dragend", marker_moved);
	GEvent.addListener(marker, "click", marker_onclick);
	feedmap.addOverlay(marker);
	return marker;
}

function centre_marker() {
	if(feedmap) {
		if(marker) {
			marker.setLatLng(feedmap.getCenter());
		} else {
			marker = new_marker(feedmap.getCenter());
		}
		marker_moved();
	}
}

function map_to_marker() {
	if(marker && feedmap)
		feedmap.setCenter(marker.getLatLng(), feedmap.getZoom());
}

function load_markers() {
	GDownloadUrl("/plain/Anonymous/json", function(data, responseCode) {parse_json(data);});
}

function marker_text_keyup() {
	var h = infowindow_text();
	var w = feedmap.getInfoWindow() ;
	var f = w.Dd[0];
	var c = f.contentElem;
	if(c.innerHTML != h) {
var a = 1;
		marker_onclick() ;
	}
}

function reveal_map() {
	var e = document.getElementsByClassName("fmapholder", document.getElementById("uploadform"));
	for(var m in e) {
		if(e[m].style)
			e[m].style.display="inline";
	}
	document.getElementById("markertxt").onkeyup = marker_text_keyup;
	startup();
	return false;
}

function given() {
	if(!feedmap) return true;

	u = document.getElementById("fzoom")
	if(u) u.value = feedmap.getZoom()
	u = document.getElementById("maptype")
	if(u) {
		var mt = feedmap.getCurrentMapType()
		u.value = mt.getName(true)
	}
	return true;
}
